﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using System.Reflection;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.SearchProperty
{
    public partial class EditSearchPropertyItem : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected string uid;
        protected string pid;
        protected string cid;

        protected ShopSearchPropertyGroup _group { get; set; }

        protected ShopSearchPropertyItem _shopCategorySearchPropertyItem;

        protected IList<ShopSearchPropertyGroup> hasChildrenGroup { get; set; }
        protected IList<ShopSearchPropertyItem> groupItemList { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                ParamKit.Bind("CategorySearchPropertyType", ddl_property_type, null, null, null, "-1", "-1");
                if (_shopCategorySearchPropertyItem != null)
                {
                    txt_property_name.Text = _shopCategorySearchPropertyItem.PropertyName;
                    txt_property_value.Text = _shopCategorySearchPropertyItem.PropertyValue;
                    txt_search_value.Text = _shopCategorySearchPropertyItem.SearchValue;
                    txt_sort.Text = _shopCategorySearchPropertyItem.Sort.ToString();
                    ddl_property_type.SelectedValue = _shopCategorySearchPropertyItem.PropertyType;
                    items.SelectedValue = _shopCategorySearchPropertyItem.ParentId.ToString();
                }
                else
                {
                    ddl_property_type.SelectedValue = "1";
                }
            }
        }

        protected void InitializeGlobalValue()
        {
            try
            {
                if (!IsPostBack)
                {
                    uid = string.IsNullOrEmpty(Request["uid"]) ? "0" : Request["uid"].ToString();
                    ViewState["uid"] = uid;

                    if (string.IsNullOrWhiteSpace(Request["pid"]))
                    {
                        ViewState["pid"] = null;
                        return;
                    }
                    pid = Request["pid"];
                    ViewState["pid"] = pid;

                    if (string.IsNullOrWhiteSpace(Request["cid"]))
                    {
                        ViewState["cid"] = null;
                        return;
                    }
                    cid = Request["cid"];
                    ViewState["cid"] = cid;
                }
                else
                {
                    if (ViewState["uid"] == null)
                    {
                        return;
                    }
                    uid = ViewState["uid"].ToString();

                    if (ViewState["pid"] == null)
                    {
                        return;
                    }
                    pid = ViewState["pid"].ToString();

                    if (ViewState["cid"] == null)
                    {
                        return;
                    }
                    cid = ViewState["cid"].ToString();
                }
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _shopCategorySearchPropertyItem = session.CreateCriteria<ShopSearchPropertyItem>().Add(Expression.Eq("Id", int.Parse(uid))).UniqueResult<ShopSearchPropertyItem>();

                    _group = session.CreateCriteria<ShopSearchPropertyGroup>().Add(Expression.Eq("Id", int.Parse(pid))).UniqueResult<ShopSearchPropertyGroup>();
                    if (_group.ParentId > 0)
                    {
                        items.DataSource = session.CreateCriteria<ShopSearchPropertyItem>().Add(Expression.Eq("SearchPropertyId", _group.ParentId)).List<ShopSearchPropertyItem>();
                        items.DataTextField = "PropertyName";
                        items.DataValueField = "Id";
                        items.DataBind();
                    }

                }

                if (_shopCategorySearchPropertyItem == null && uid != "0")
                {
                    throw new Exception("找不到对应信息.");
                }
            
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (_shopCategorySearchPropertyItem == null)
                    {
                        _shopCategorySearchPropertyItem = new ShopSearchPropertyItem();
                    }
                    _shopCategorySearchPropertyItem.SearchPropertyId = Convert.ToInt32(pid);
                    _shopCategorySearchPropertyItem.CategoryId = Convert.ToInt32(cid);
                    _shopCategorySearchPropertyItem.PropertyName = txt_property_name.Text;
                    _shopCategorySearchPropertyItem.PropertyValue = txt_property_value.Text;
                    _shopCategorySearchPropertyItem.SearchValue = txt_search_value.Text;
                    _shopCategorySearchPropertyItem.Sort = Convert.ToInt32(txt_sort.Text);
                    _shopCategorySearchPropertyItem.PropertyType = ddl_property_type.Text;
                    try
                    {
                        _shopCategorySearchPropertyItem.ParentId = int.Parse(Request["items"]);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    //   if (string.IsNullOrEmpty(_shopCategorySearchPropertyItem.PropertyName) || string.IsNullOrEmpty(_shopCategorySearchPropertyItem.PropertyValue) || string.IsNullOrEmpty(_shopCategorySearchPropertyItem.SearchValue))
                    if (string.IsNullOrEmpty(_shopCategorySearchPropertyItem.PropertyName) )
                    {
                        throw new Exception("必填项为空.");
                    }

                    //判断属性项目信息是否重复
                  //   int count=Convert.ToInt32( session.CreateQuery("select count(*) from ShopSearchPropertyItem where ( PropertyName=:name or PropertyValue=:value or SearchValue=:svalue ) and Id!=:uid").SetParameter("name", _shopCategorySearchPropertyItem.PropertyName ).SetParameter("value",_shopCategorySearchPropertyItem.PropertyValue).SetParameter("svalue",_shopCategorySearchPropertyItem.SearchValue).SetParameter("uid",uid).UniqueResult());
                   //int count = Convert.ToInt32(session.CreateQuery("select count(*) from ShopSearchPropertyItem where  PropertyName=:name   and Id!=:uid").SetParameter("name", _shopCategorySearchPropertyItem.PropertyName).SetParameter("uid", uid).UniqueResult());
                   // if (count > 0)
                   // {
                   //     throw new Exception("属性项目信息重复!.");
                   // }

                    session.SaveOrUpdate(_shopCategorySearchPropertyItem);
                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = "EditSearchPropertyItem"
            });
        }
    }
}
